<?php
    date_default_timezone_set("Asia/Saigon");
    header('Content-Type: text/html; charset=UTF-8');
    require_once("config.php");     
    require_once("functions.php");     
    
   $mo = isset($_GET['mo'])?$_GET['mo'] :"";
   $serviceNumber = isset($_GET['serviceNumber'])?$_GET['serviceNumber'] :"";
   $mobile = isset($_GET['mobile'])?$_GET['mobile'] :"";
   $requestId = isset($_GET['requestId'])?$_GET['requestId'] :"";
   
    // Mo format: CMDCode OSID APPID|APPHeader [UserID contentId Time] Refcode 
    // Danh sach cmd code dich vu
    $arrClip = array("TCLIP","MCLIP");
    $arrComic = array("TTRUYEN","MTRUYEN");
    $arrAudio = array("TAUDIO","MAUDIO");
    $arrSmsKute = array("TKUTE","MKUTE");
    $arrXoso = array("TXOSO","MXOSO");
    $arrKichHoatAPK = array("TKGAME","MKGAME");
    $arrKichHoatJava = array("TJGAME","MJGAME");
    $arrGameOnline = array("MTPDTD","MTPVTL","MTPKH1","MTPKH2","MTPKH3","MTPKH4","MTPKH5","MTPKH6","MTPTNO");
    $arrMusic = array("TMCHO","MMCHO");
    $arrTuVi = array("MTPMT1");
    $arrGameOffline = array("","");
    $arrSystem = array("MTAI","MPASS");
    $arrayCMDCode = array("video"=>$arrClip,"audio"=>$arrAudio,"comic"=>$arrComic,"smskute"=>$arrSmsKute,"xoso"=>$arrXoso
    ,"kichhoatapk"=>$arrKichHoatAPK,"kichhoatjar"=>$arrKichHoatJava,"gameonline"=>$arrGameOnline,"gameoffline"=>$arrGameOffline,"music"=>$arrMusic,"system"=>$arrSystem,"tuvi"=>$arrTuVi);   
    // Constants dich vu
        
   if(empty($mo)||empty($serviceNumber)||empty($mobile)||empty($requestId)){
       echo "Loi cu phap.";
   }else{
         $mo = trim($mo);
         $mo = preg_replace("/(\s+)/si", " ", $mo);
         $arrMo = explode(" ", $mo);
       
         $cmdCode= $arrMo[0];
         $osId= intval($arrMo[1]);
         $appHeaderMO= $arrMo[2];
         $refcode = $arrMo[count($arrMo)-1];
         
         // Lay loai dich vu qua cmd code
         $keyservice = "";
         $checkCmd = 0;
         foreach($arrayCMDCode as $key => $values){
             foreach ($values as $item ){
                  if(strcasecmp($item,$cmdCode)==0) {
                      $checkCmd = 1;
                      break;
                  }                  
             }
             
             if($checkCmd==1) {$keyservice =$key;break;}
         }
         $data = array();
         $data["transactionId"] = $requestId;
         $data["mobile"] = $mobile;
         $data["partnerId"] = 0;
        
         $userrow = Null;
         // Vivo lay user qua id thay cho refcode update 22/01/2014
         if(strcasecmp($cmdCode,"MTPDTD")==0||strcasecmp($cmdCode,"MTPVTL")==0||strcasecmp($cmdCode,"MTPTNO")==0){
              $userrow  = getUserInfo(intval($refcode));
         }   
          
         if($userrow == Null){
             if(strlen($refcode)==32)
            $userrow  = getUserByRefCode($refcode);
             else{
                $refcode = substr($refcode,3);
                $userrow  = getUserByRefCode($refcode);
             } 
         
         }
         
 
         if(!empty($userrow)) 
         $data["userId"] = $userrow["id"]; else $data["userId"] = 0;
         
         $appHeader = getAppIdByAppHeader($appHeaderMO);
         $data["appId"] = $appHeader;
         $data["dataId"] = 0;
         $data["mo"] = $mo;
         $data["telco"]  =checkTelco($mobile);
         $data["serviceNumber"] = $serviceNumber;
         $data["price"] = priceServiceCode($serviceNumber);
         $data["status"]  = 1;
         $data["os_type"] = $osId;
         $data["action"] = "";
         $data["percent_wapmaster"] = 0;
         $data["percent_partner"] = 0;
         $data["penalty_mt"] = 0;
         $data["status_mo"] = 0;
         $data["status_send_mt"] = 1; // da gui MT
         $data["type"] = 0;
         
         $mt = "Cam on ban da su dung dich vu. Chuc ban co nhung phut giay thu gian vui ve.";
         
        // echo $keyservice;
       
         switch($keyservice){
             case "video":
              $data["type"] = 3;
              $rowApp = getAppInfo($appHeader);
              if(!empty($rowApp)){
                  $percent_wapmaster=$rowApp["percent_wapmaster"];$percent_partner=$rowApp["percent_partner"];
                  $data["percent_wapmaster"] = $percent_wapmaster;
                  $data["percent_partner"] = $percent_partner;
              }
              $mt = "Cam on ban da su dung dich vu video. Chuc ban co nhung phut giay thu gian vui ve.";
             break;
             
             case "audio":
                $data["type"] = 4;
                $rowApp = getAppInfo($appHeader);
                if(!empty($rowApp)){
                  $percent_wapmaster=$rowApp["percent_wapmaster"];$percent_partner=$rowApp["percent_partner"];
                  $data["percent_wapmaster"] = $percent_wapmaster;
                  $data["percent_partner"] = $percent_partner;
              }
                $mt = "Cam on ban da su dung dich vu truyen audio. Chuc ban co nhung phut giay thu gian vui ve.";
             break;
             
             case "comic":
                $data["type"] = 4;
                $data["appId"] = 22; 
                $rowApp = getAppInfo($appHeader);
               if(!empty($rowApp)){
                  $percent_wapmaster=$rowApp["percent_wapmaster"];$percent_partner=$rowApp["percent_partner"];
                  $data["percent_wapmaster"] = $percent_wapmaster;
                  $data["percent_partner"] = $percent_partner;
              }
               // $mt = "Cam on ban da su dung dich vu doc truyen. Chuc ban co nhung phut giay thu gian vui ve.";
                $userComicId= intval($arrMo[3]);
               
                $mt = getMTTruyenTranh($data["mo"],$data["serviceNumber"],$userComicId);
                
               
             break;
             
             case "smskute":
                $data["type"] = 4;
                $rowApp = getAppInfo($appHeader);
               if(!empty($rowApp)){
                  $percent_wapmaster=$rowApp["percent_wapmaster"];$percent_partner=$rowApp["percent_partner"];
                  $data["percent_wapmaster"] = $percent_wapmaster;
                  $data["percent_partner"] = $percent_partner;
              }
                $mt = "Cam on ban da su dung dich vu smskute. Chuc ban co nhung phut giay thu gian vui ve.";
             break;
             
             case "kichhoatapk":
                $data["appId"] = 0; 
                $data["type"] = 5;
                $data["percent_wapmaster"] = 88;
                $data["percent_partner"] = 0;
                $mt = "Cam on ban da su dung dich vu choi game. Chuc ban co nhung phut giay thu gian vui ve.";
             break;
             
             case "kichhoatjar":
                $data["appId"] = 0; 
                $data["type"] = 6;
                $data["percent_wapmaster"] = 88;
                $data["percent_partner"] = 0;
                $mt = "Cam on ban da su dung dich vu choi game. Chuc ban co nhung phut giay thu gian vui ve.";
             break;
             
             case "gameonline":
                $data["type"] = 1;
                // get % theo arry dau so va cu phap -> game -> %
                 $mt = "Cam on ban da su dung dich vu choi game online. Chuc ban co nhung phut giay thu gian vui ve.";
                //Vivoo MTPTNO 
                if(strcasecmp($cmdCode,"MTPDTD")==0||strcasecmp($cmdCode,"MTPVTL")==0||strcasecmp($cmdCode,"MTPTNO")==0){
                   $moid = $requestId;$src=$mobile;$dest=$serviceNumber;$msgbody=$mo ;
                   $contentMT = getMTFromViVoo($moid,$src,$dest,$cmdCode,$msgbody);
                   $arrContentMT = explode("|", $contentMT);
                   if($arrContentMT[2]==""||$arrContentMT[2]==Null) $arrContentMT[2]="Loi MT";
                   $mt = $arrContentMT[2];
                   $gameId = getGameIdByCMDCodeMeCord($cmdCode);// suy ra tu cmdcode -> dat comandcode vao game
                   $rowGame = getGameInfo($gameId);
                   $data["appId"] = $gameId; //se bang ID game
                   $data["status"]  = intval($arrContentMT[1]); // 1 là tinh tien 0 la kong
                   $data["mt"] = $arrContentMT[2];
                   $data["partnerId"] = $rowGame["partnerId"] ;
                   $data["os_type"] = $osId;
                   $data["action"] = "";
                   $data["percent_wapmaster"] = $rowGame["percent_wapmaster"];
                   $data["percent_partner"] = $rowGame["percent_partner"];
               }
               
             
                //Khanh
               if(strcasecmp($cmdCode,"MTPKH1")==0||strcasecmp($cmdCode,"MTPKH2")==0||strcasecmp($cmdCode,"MTPKH3")==0
               ||strcasecmp($cmdCode,"MTPKH4")==0||strcasecmp($cmdCode,"MTPKH5")==0||strcasecmp($cmdCode,"MTPKH6")==0){
                     
                   $contentMT = getMTFromKhanh(1,1,$requestId,"SMS",$mobile,$mo,$serviceNumber,priceServiceCode($serviceNumber));
                   $contentMT= str_replace("\"","",$contentMT);
                   $arrContentMT = explode("|", $contentMT);
                   if($arrContentMT[1]==""||$arrContentMT[1]==Null) $arrContentMT[1]="Loi MT";
                   $mt = $arrContentMT[1];
                   
                   $gameId = getGameIdByCMDCodeMeCord($cmdCode);// suy ra tu cmdcode -> dat comandcode vao game
                   $rowGame = getGameInfo($gameId);
                   $data["appId"] = $gameId; //se bang ID game
                   $data["status"]  = intval($arrContentMT[0]); // 1 là tinh tien 0 la kong
                   $data["mt"] = $arrContentMT[2];
                   $data["partnerId"] = $rowGame["partnerId"] ;
                   $data["os_type"] = $osId;
                   $data["action"] = "";
                   $data["percent_wapmaster"] = $rowGame["percent_wapmaster"];
                   $data["percent_partner"] = $rowGame["percent_partner"];
               }
                 
                
             break;
             
             case "gameoffline":
                $data["type"] = 2;
                // get % theo arry dau so va cu phap -> game -> %
                $mt = "Cam on ban da su dung dich vu game offline. Chuc ban co nhung phut giay thu gian vui ve.";
             break;
             
             case "music":
                $data["type"] = 4;
                $rowApp = getAppInfo($appHeader);
                if(!empty($rowApp)){
                  $percent_wapmaster=$rowApp["percent_wapmaster"];$percent_partner=$rowApp["percent_partner"];
                  $data["percent_wapmaster"] = $percent_wapmaster;
                  $data["percent_partner"] = $percent_partner;
                }
                // get % theo arry dau so va cu phap -> game -> %
                $mt = "Cam on ban da su dung dich vu nhac. Chuc ban co nhung phut giay thu gian vui ve.";
             break;
             
             case "xoso":
                $data["type"] = 4;
                $data["appId"] = 13;
                $appHeader = 13;
                $rowApp = getAppInfo($appHeader);
                if(!empty($rowApp)){
                      $percent_wapmaster=$rowApp["percent_wapmaster"];$percent_partner=$rowApp["percent_partner"];
                      $data["percent_wapmaster"] = $percent_wapmaster;
                      $data["percent_partner"] = $percent_partner;
                }
                $mt = "Cam on ban da su dung dich vu xo so. Chuc ban may man.";
             break;
             
             case "tuvi":
                $data["type"] = 4;
                $data["appId"] = 23;
                $appHeader = 23;
                $rowApp = getAppInfo($appHeader);
                if(!empty($rowApp)){
                      $percent_wapmaster=$rowApp["percent_wapmaster"];$percent_partner=$rowApp["percent_partner"];
                      $data["percent_wapmaster"] = $percent_wapmaster;
                      $data["percent_partner"] = $percent_partner;
                }
                $mt = "Cam on ban da su dung dich vu tu vi. Chuc ban may man.";
             break;
             
             case "system":
             //  Lay link ung dung game
                 if(strcasecmp("mtai",$cmdCode)==0){
                    $mt ="Link tai khong ton tai. Vui long kiem tra lai.";
                    if(count($arrMo)>3){
                        $data["userId"] = intval($arrMo[3]);
                        $cmdCode= $arrMo[0];
                        $subCode =strtolower($arrMo[1]);
                        $clientId = intval($arrMo[2]);
                        $userId = intval($arrMo[3]);  
                        if(strcmp("g",$subCode)==0){
                            $userInfo = getUserInfo($userId);
                            $appInfo =  getGameInfo($clientId);
                           // var_dump($subCode);die;
                            if($userInfo == Null){
                                $mt = "Tai khoan khong ton tai. Vui long kiem tra lai.";
                            }else if($appInfo==Null){
                                $mt = "Game khong ton tai. Vui long kiem tra lai.";
                            }else{
                                $mt = "http://mtop.vn/game/download-".$clientId."-".$userId;    
                            }
                        }
                            
                        if(strcmp("a",$subCode)==0){
                            $userInfo = getUserInfo($userId);
                            $appInfo =  getAppInfo($clientId);
                            if($userInfo == Null){
                                $mt = "Tai khoan khong ton tai. Vui long kiem tra lai.";
                            }else if($appInfo==Null){
                                $mt = "Ung dung khong ton tai. Vui long kiem tra lai.";
                            }else{
                                $mt = "http://mtop.vn/ung-dung/download-".$clientId."-".$userId;
                            }
                        }
                    }
                }
                // Get Password
                if(strcasecmp("MPASS",$cmdCode)==0){
                    $data["userId"] = intval($arrMo[3]);
                     $cmdCode= $arrMo[0];
                     $username = $arrMo[1];
                     $mt = "Yeu cau khong duoc xu ly. Vui long kiem tra lai. Hoac lien he voi bo phan ho tro.";
                     if(count($arrMo)>1){
                         $userInfo = getCUserByMobile($mobile);
                         if($userInfo==Null){
                             $mt = "Tai khoan khong ton tai. Vui long kiem tra lai.";
                         }else{
                             $pass = "123456";
                             $pass =$pass.substr("".time(),strlen(time())-3);
                             $result = UpdateUserPass($userInfo["id"],$pass);
                             if(intval($result)==1)
                             $mt = "Mat khau moi cua ban la: ".$pass;
                         }
                     }
                }
                $data["type"] = 4;
                $data["appId"] = 0;
                $data["percent_wapmaster"] = 70;
                $data["percent_partner"] = 0;
             break;
             default:
                $data["type"] = 0;
                $mt = "Cam on ban da su dung dich vu. Chuc ban co nhung phut giay thu gian vui ve.";
             break;
         }
         $data["mt"] = $mt;
         
         if(checkTransactionExist($data["transactionId"] )==0)
            InsertSmsLog($data);
         else
            UpdateSmsLog($data);
         
         $datalog = "requestId=".$requestId."|mo=".$mo."|"."serviceNumber=".$serviceNumber."|"."mobile=".$mobile."|"."mt=".$mt."|appHeader=".$appHeader;
         writeLog($datalog,"/logSms.txt");
         echo $mt;
   }
?>
